//
//  UIView+LXExpanding.m
//  TestDemo
//
//  Created by xiao.zhao@lachesis-mh.com on 16/2/18.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "UIView+LXExpanding.h"

@implementation UIView (LXExpanding)

-(void)setLx_origin:(CGPoint)lx_origin
{
    CGRect frame = self.frame;
    frame.origin = lx_origin;
    self.frame = frame;
}

-(CGPoint)lx_origin
{
    return self.frame.origin;
}

-(void)setLx_originX:(CGFloat)lx_originX
{
    CGRect frame = self.frame;
    frame.origin.x = lx_originX;
    self.frame = frame;
}

-(CGFloat)lx_originX
{
    return self.frame.origin.x;
}

-(void)setLx_originY:(CGFloat)lx_originY
{
    CGRect frame = self.frame;
    frame.origin.y = lx_originY;
    self.frame = frame;
}

-(CGFloat)lx_originY
{
    return self.frame.origin.y;
}

-(void)setLx_size:(CGSize)lx_size
{
    CGRect frame = self.frame;
    frame.size = lx_size;
    self.frame = frame;
}

-(CGSize)lx_size
{
    return self.frame.size;
}

-(void)setLx_width:(CGFloat)lx_width
{
    CGRect frame = self.frame;
    frame.size.width = lx_width;
    self.frame = frame;
}

-(CGFloat)lx_width
{
    return self.frame.size.width;
}

-(void)setLx_height:(CGFloat)lx_height
{
    CGRect frame = self.frame;
    frame.size.height = lx_height;
    self.frame = frame;
}

-(CGFloat)lx_height
{
    return self.frame.size.height;
}

- (CGFloat)lx_top {
    
    return self.frame.origin.y;
}

- (void)setLx_top:(CGFloat)top {
    
    CGRect newFrame   = self.frame;
    newFrame.origin.y = top;
    self.frame        = newFrame;
}

- (CGFloat)lx_bottom {
    
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setLx_bottom:(CGFloat)bottom {
    
    CGRect newFrame   = self.frame;
    newFrame.origin.y = bottom - self.frame.size.height;
    self.frame        = newFrame;
}

- (CGFloat)lx_left {
    
    return self.frame.origin.x;
}

- (void)setLx_left:(CGFloat)left {
    
    CGRect newFrame   = self.frame;
    newFrame.origin.x = left;
    self.frame        = newFrame;
}

- (CGFloat)lx_right {
    
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setLx_right:(CGFloat)right {
    
    CGRect newFrame   = self.frame;
    newFrame.origin.x = right - self.frame.size.width;
    self.frame        = newFrame;
}

- (CGFloat)lx_centerX {
    
    return self.center.x;
}

- (void)setLx_centerX:(CGFloat)centerX {
    
    CGPoint newCenter = self.center;
    newCenter.x       = centerX;
    self.center       = newCenter;
}

- (CGFloat)lx_centerY {
    
    return self.center.y;
}

- (void)setLx_centerY:(CGFloat)centerY {
    
    CGPoint newCenter = self.center;
    newCenter.y       = centerY;
    self.center       = newCenter;
}

- (CGFloat)lx_middleX {
    
    return CGRectGetWidth(self.bounds) / 2.f;
}

- (CGFloat)lx_middleY {
    
    return CGRectGetHeight(self.bounds) / 2.f;
}

- (CGFloat)middleX {
    
    return CGRectGetWidth(self.bounds) / 2.f;
}

- (CGFloat)middleY {
    
    return CGRectGetHeight(self.bounds) / 2.f;
}

- (CGPoint)lx_middlePoint {
    return CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
}

- (CGFloat)lx_maxX {
    return self.lx_x + self.lx_width;
}

- (CGFloat)lx_maxY {
    return self.lx_y + self.lx_height;
}

- (CGFloat)lx_y {
    return CGRectGetMinY(self.frame);
}

- (CGFloat)lx_x {
    return CGRectGetMinX(self.frame);
}

- (void)setLx_y:(CGFloat)Y{
    CGRect rc = self.frame;
    rc.origin.y = Y;
    self.frame = rc;
}

- (void)setLx_x:(CGFloat)X{
    CGRect rc = self.frame;
    rc.origin.x = X;
    self.frame = rc;
}

NSString * const _recognizerScale = @"_recognizerScale";

- (void)setScale:(CGFloat)scale {
    
    objc_setAssociatedObject(self, (__bridge const void *)(_recognizerScale), @(scale), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.transform = CGAffineTransformMakeScale(scale, scale);
}

- (CGFloat)scale {
    
    NSNumber *scaleValue = objc_getAssociatedObject(self, (__bridge const void *)(_recognizerScale));
    return scaleValue.floatValue;
}

#pragma mark - Angle.

NSString * const _recognizerAngle = @"_recognizerAngle";

- (void)setAngle:(CGFloat)angle {
    
    objc_setAssociatedObject(self, (__bridge const void *)(_recognizerAngle), @(angle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.transform = CGAffineTransformMakeRotation(angle);
}

- (CGFloat)angle {
    
    NSNumber *angleValue = objc_getAssociatedObject(self, (__bridge const void *)(_recognizerAngle));
    return angleValue.floatValue;
}

#pragma mark -
#pragma mark -HUD
-(void)showHudWithText:(NSString *)text
{
    [self showHudWithText:text hideDelay:0];
}

-(void)showHudWithText:(NSString *)text hideDelay:(NSTimeInterval)delay
{
    if ([text isEqualToString:@""]) {
        [self showHudWithText:nil hideDelay:delay complete:nil];
    } else {
        NSString *title = @"";
        NSString *content = text;
        MessageViewObject *messageObject = MakeMessageViewObject(title, content);
        [self showHudWithText:messageObject hideDelay:delay complete:nil];
    }
}

-(void)showHudWithText:(MessageViewObject *)message hideDelay:(NSTimeInterval)delay complete:(void(^)())complete
{
    if (delay > 0) {
        [LXLoadingView showAutoHiddenMessageViewInKeyWindowWithMessageObject:message delegate:self viewTag:arc4random() % 100 delayAutoHidenDuration:delay];
    } else {
        [LXLoadingView showAutoHiddenMessageViewInKeyWindowWithMessageObject:message delegate:self viewTag:arc4random() % 100 delayAutoHidenDuration:15];
    }
}

-(void)changeHudWithText:(NSString *)text
{
    
}

-(void)hideHud
{
    [LXLoadingView hideHUDForViewMessage:self];
}

-(void)showWarnMessage:(NSString *)message
{
    NSString *title = @"";
    NSString *content = message;
    MessageViewObject *messageObject = MakeMessageViewObject(title, content);
    [LXMessageView showAutoHiddenMessageViewInKeyWindowWithMessageObject:messageObject delegate:self viewTag:arc4random() % 100];
}

-(void)showWarnAlertObject:(NSString *)title buttonArray:(NSArray *)buttonArrays withDelegate:(id <LXBaseMessageViewDelegate>)delegate
{
    NSMutableArray *sendArray = [[NSMutableArray alloc] init];
    [buttonArrays enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        AlertViewButtonStyle *style = [AlertViewButtonStyle new];
        if (idx == 0) {
            style.title                 = obj;
            style.style                 = kAlertViewRedStyle;
        } else {
            style.title                 = obj;
            style.style                 = kAlertViewNormalStyle;
        }
        [sendArray addObject:style];
    }];
    AlertViewMessageObject *message = MakeAlertViewMessageObject(title, sendArray);
    [LXHubAlertView showManualHiddenMessageViewInKeyWindowWithMessageObject:message delegate:delegate viewTag:arc4random() % 100];
}

@end
